package com.yyfax.ph.personal.message;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.infrastructure.network.entity.EmptyEntity;
import com.yyfax.ph.base.YJPH;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

import java.util.List;

public class MsgModel extends Model {

	private static final String TAG = MsgModel.class.getSimpleName();

	static final int MSG_REFRESH = 15;
	static final int MSG_LOAD_MORE = 16;
	static final int MSG_CHECK_INFO = 17;
	static final String MESSAGE_UNREAD = "0";
	static final String MESSAGE_READ = "1";

	static final String TARGET_PROGRESS = "100";
	static final String TARGET_SUBACCOUNT = "101";
	static final String TARGET_CASH_DETAIL = "102";

	@Override
	public void onNext(Object obj) {
		CLog.i(TAG, "onNext: " + obj.toString());
		BaseResultEntity entity = cast(obj);
		switch (entity.requestCode) {
			case MSG_CHECK_INFO:
				MsgStatusResult msgStatusResult = cast(obj);
				EventAgent.post(msgStatusResult);
				break;
			case MSG_REFRESH:
			case MSG_LOAD_MORE:
				MsgInfoResult msgInfoResult = cast(obj);
				EventAgent.post(msgInfoResult);
				break;
		}

	}

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		String token = YJPH.getInstance().getPreferenceManager().getLoginToken();
		switch (functionalCode) {
			case MSG_CHECK_INFO:
				return service.checkMsgStatus(token, new EmptyEntity());
			case MSG_REFRESH:
			case MSG_LOAD_MORE:
				return service.getMsgInfo(token, (MsgParam) params);
		}
		return null;
	}


	public interface Service {
		@POST("sys/letter/list")
		Observable<MsgInfoResult> getMsgInfo(@Header("token") String token, @Body MsgParam params);

		/**
		 * 红点
		 */
		@POST("sys/letter/status")
		Observable<MsgStatusResult> checkMsgStatus(@Header("token") String token, @Body EmptyEntity params);
	}

	public static class MsgParam {
		String pageNo;
		String pageSize;
	}

	public static class MsgInfoResult extends BaseResultEntity<List<MsgInfo>> {

	}

	public static class MsgInfo {
		String id;//ID
		String sender;//发件人
		String title;//标题
		String content;//内容
		String status;//状态 0：未读 1：已读
		String createDate;// 发送日期
		String target;//跳转目标
		String refId;//跳转至进度详情时 该条进度详情的id

		@Override
		public String toString() {
			return "MsgInfo{" + "id='" + id + '\'' + ", sender='" + sender + '\'' + ", title='" + title + '\'' + ", content='" + content + '\'' + ", status='" + status + '\'' + ", createDate='" + createDate + '\''+ ", target='" + target + '\'' + '}';
		}
	}

	public static class MsgStatusResult extends BaseResultEntity<String> {
		//content  状态 0：全部已读 1：存在未读
	}


}
